package com.gaoyi.crm.fragment.mtc;

import androidx.activity.result.ActivityResult;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gaoyi.crm.R;
import com.gaoyi.crm.adapter.NoSyncPmnAdapter;
import com.gaoyi.crm.callback.IView;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.control.dialog.LoadingDialogUtils;
import com.gaoyi.crm.databinding.FragmentMtvNoSyncListBinding;
import com.gaoyi.crm.db.entity.PmnWithList;
import com.gaoyi.crm.db.query.MtcSyncedQuery;
import com.gaoyi.crm.fragment.base.BaseChildListFragment;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.util.Utils;
import com.gaoyi.crm.vm.MtcNoSyncListViewModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MtcNoSyncListFragment extends BaseChildListFragment<FragmentMtvNoSyncListBinding, MtcNoSyncListViewModel, PmnWithList, MtcSyncedQuery> implements IView {

    private NoSyncPmnAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mtv_no_sync_list;
    }

    @Override
    protected void initView() {
        mDateSearchView = mBinding.mobileInclude.viewDateSearch;
        mViewSearch = mBinding.mobileInclude.viewSearch;
        mSmartRefreshLayout = mBinding.smartRefreshLayout;
        serTitleSort(mBinding.titleCreateDate, mBinding.titleModifyDate);
        mAdapter = new NoSyncPmnAdapter(mContext);
        mAdapter.setOnItemClickListener((v, position) -> mAdapter.changeSelected(position));
        mBinding.rvNoSync.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rvNoSync.setItemAnimator(null);
        mBinding.rvNoSync.setAdapter(mAdapter);
        mBinding.btnSync.setOnClickListener(view -> sync());
    }

    private void sync() {
        List<PmnWithList> list = mAdapter.getCurrentList();
        List<PmnWithList> checkList = new ArrayList<>();
        for (PmnWithList pmnWithList : list) {
            if (pmnWithList.isCheck()) {
                checkList.add(pmnWithList);
            }
        }
        if (checkList.isEmpty()) {
            ToastUtil.showToast(R.string.p_sel_sync_pmn);
            return;
        }
        mViewModel.syncPmn(checkList);
    }

    @Override
    protected void initData() {
        if (isPause) { // 跳转后返回，SearchView会自动调用onQueryTextChange请求数据
            return;
        }
        showLoading();
        mQuery.init();
        mQuery.setSyncedStatus(ApiStatus.NO_UPLOAD);
        mViewModel.getNoSyncPmn(mQuery);
    }

    @Override
    protected void initLiveData() {
        mViewModel.getTotal().observe(mParent, integer -> mBinding.keyValueNoSync.setValue(new String[]{getString(R.string.total_item, integer)}));
        mViewModel.getResultCode().observe(mParent, integer -> Utils.refreshSmartRefreshLayout(mBinding.smartRefreshLayout));
        mViewModel.getList().observe(mParent, pmnSyncDtos -> mAdapter.submitList(pmnSyncDtos));
    }

    @Override
    protected void loadData() {
        mViewModel.getNoSyncPmn(mQuery);
    }

    @Override
    protected void search(Date startDate, Date endDate) {
        mViewModel.clearListValue();
        mQuery.setEndDate(endDate.getTime());
        mQuery.setStartDate(startDate.getTime());
        mQuery.setKeyWord(mViewSearch.getQuery().toString());
        mQuery.init();
        mViewModel.getNoSyncPmn(mQuery);
    }

    @Override
    protected void search(String keyWord) {
        mViewModel.clearListValue();
        if (mDateSearchView.isSelect()) {
            mQuery.setStartDate(mDateSearchView.getStartDate().getTime());
            mQuery.setEndDate(mDateSearchView.getEndDate().getTime());
        }
        mQuery.setKeyWord(keyWord);
        mQuery.init();
        mViewModel.getNoSyncPmn(mQuery);
    }

    @Override
    protected void sort(int id, Integer sort) {
        if (id == R.id.title_create_date) {
            mQuery.setOrderByCreatedAt(sort);
        } else if (id == R.id.title_modify_date) {
            mQuery.setOrderByModifyAt(sort);
        }
        mViewModel.clearListValue();
        mQuery.init();
        mViewModel.getNoSyncPmn(mQuery);
    }

    @Override
    public void onPause() {
        super.onPause();
        isPause = true;
    }

    @Override
    protected MtcSyncedQuery createQuery() {
        return new MtcSyncedQuery();
    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(mContext);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }

}